<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class BoothModel extends CI_Model 
{ 
	private $_table = 'booths';
  	private $_primary = 'id';
  
  	function add($data)
  	{
  		$this->db->insert($this->_table, $data);
  		return $this->db->insert_id();
  	}
  	
	function update($id, $data)
  	{
    	$this->db->where($this->_primary, $id);
    	return $this->db->update($this->_table, $data);
  	} 
  	
  	/*
  	 * Trả lại hiển thị chi tiết một gian hàng
  	 * @param string $slug
  	 */
  	function getBoothBySlug($slug)
  	{
  	  $query = $this->db->query("SELECT * FROM  `{$this->_table}`
  	      WHERE `slug` = '{$slug}'");
  	      return $query->row_array();
  	}
  	
  	public function getBooths($limit, $start)
  	{
  	    $query = $this->db->query("SELECT * FROM `{$this->_table}`
  	        ORDER BY `{$this->_primary}` DESC
  	        LIMIT {$start}, {$limit}");
  	    return $query->result_array();
  	}
        
    public function getBoothById($id)
  	{
  	    $query = $this->db->query("SELECT * FROM `{$this->_table}`
  	        WHERE `id` = {$id}");
  	    return $query->row_array();
  	}

  	function getBoothByUserId($user_id)
  	{
  	    $query = $this->db->query("SELECT *
  	        FROM  `{$this->_table}`
  	        WHERE `user_id` = {$user_id}");
  	    return $query->row_array();
  	}
  	
  	function getProductsByBoothId($id)
  	{
  	    $query = $this->db->query("SELECT *
  	        FROM  `booth_products`
  	        WHERE `booth_id` = {$id}");
  	    return $query->result_array();
  	}
  	
  	function getProductBySlug($slug)
  	{
  	    $query = $this->db->query("SELECT *
  	        FROM  `booth_products`
  	        WHERE `slug` = '{$slug}'");
  	    return $query->row_array();
  	}
  	
  	function addProduct($data)
  	{
  	    $this->db->insert('booth_products', $data);
  	}
  	
  	function updateProduct($id, $data)
  	{
  	    $this->db->where('id', $id);
  	    return $this->db->update('booth_products', $data);
  	}
  	
  	function addProductCategory($data)
  	{
        $this->db->insert('booth_product_categories', $data);
  	}
  	
  	function getBoothProductCategoriesByBoothId($id)
  	{
  	    $query = $this->db->query("SELECT *
  	        FROM  `booth_product_categories`
  	        WHERE `booth_id` = {$id}");
  	    return $query->result_array();
  	}
  	
  	/**
  	 * @view getBoothCategories() in booth_helper.php
  	 */
  	public function getBoothsByCategoryId($limit, $start, $categoryId)
  	{
  	  if ($categoryId == 1) {
  	    $categoryStr = 'IN (1,2,3,4,5,6,7,8)';
  	  } else if ($categoryId == 9) {
  	    $categoryStr = 'IN (10,11,12,13,14,15,16,17,18,19,20)';
  	  } else if ($categoryId == 21) {
  	    $categoryStr = 'IN (22,23,24,25,26,27,28,29,30,31,32,33,34,35)';
  	  } else if ($categoryId == 36) {
  	    $categoryStr = 'IN (37,38,39)';
  	  } else if ($categoryId == 40) {
  	    $categoryStr = 'IN (41,42,43,44,45,46,47,48,49,50,51,52)';
  	  } else if ($categoryId == 53) {
  	    $categoryStr = 'IN (54,55,56,57,58)';
  	  } else if ($categoryId == 59) {
  	    $categoryStr = 'IN (60,61,62,63,64,65,66,67,68,69,70,71)';
  	  } else if ($categoryId == 72) {
  	    $categoryStr = 'IN (73,74,75,76,77,78,79,80,,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97)';
  	  } else if ($categoryId == 98) {
  	    $categoryStr = 'IN (99,100,101,102,103,104)';
  	  } else if ($categoryId == 105) {
  	    $categoryStr = 'IN (106,107,108,109,110,111,112,113,114,115,116,117,118)';
  	  } else if ($categoryId == 119) {
  	    $categoryStr = 'IN (120,121,122,123,124,125,126,127,128,129,130)';
  	  } else if ($categoryId == 131) {
  	    $categoryStr = 'IN (132,133,134,135,136)';
  	  } else if ($categoryId == 137) {
  	    $categoryStr = 'IN (138,139,140,141,142,143,144)';
  	  } else if ($categoryId == 145) {
  	    $categoryStr = 'IN (146,147,148)';
  	  } else if ($categoryId == 149) {
  	    $categoryStr = 'IN (150,151,152,153,154,155,156)';
  	  } else if ($categoryId == 157) {
  	    $categoryStr = 'IN (158,159,160,161,162,163,164,165,166,167,168,169,170)';
  	  }    
  	
  	  $query = $this->db->query("SELECT * FROM `{$this->_table}`
  	      WHERE `category_id` {$categoryStr}
  	      ORDER BY `id` DESC
  	      LIMIT {$start}, {$limit}");
  	      return $query->result_array();
  	}
	
	public function deleteCategoryById($id)
  	{
    	$this->db->where('id', $id);
    	return $this->db->delete('booth_product_categories');
  	}
        
    public function getBoothHasProducts($limit, $start) {
        $query = $this->db->query("SELECT DISTINCT {$this->_table}.* 
            FROM {$this->_table}, `booth_products` 
            WHERE {$this->_table}.{$this->_primary} = `booth_products`.`booth_id`
            LIMIT {$start}, {$limit}");
        return $query->result_array();
    }
}